"""
@author: 一切、會恏
@file: log.py
@time: 2019/3/13 18:26
"""
import logging#导入日志模块
from common.public import project_path
class myLog:
    def log_info(self,level,msg):
        #定义一个日志收集器并设置级别getLogger()
        format = logging.Formatter("[%(asctime)s]-[%(levelname)s]日志信息:%(message)s")
        my_logger = logging.getLogger("python14")
        my_logger.setLevel("DEBUG")
        #指定输出渠道并设置级别和日志输出格式：控制台StreamHandler()  文本FileHandler()
        sh = logging.StreamHandler()
        sh.setLevel("INFO")
        sh.setFormatter(format)

        fh = logging.FileHandler(project_path.log_path,encoding="utf-8")
        fh.setLevel("INFO")
        fh.setFormatter(format)
        #对接   输出结果以收集器和输出渠道的交集为主 addHandler()
        my_logger.addHandler(sh)
        my_logger.addHandler(fh)
        if level.upper() == "DEBUG":#根据传递的日志等级直接输出相对应等级的提示
            my_logger.debug(msg)
        elif level.upper() == "INFO":
            my_logger.info(msg)
        elif level.upper() == "WARNING":
            my_logger.warning(msg)
        elif level.upper() == "ERROR":
            my_logger.error(msg)
        elif level.upper() == "CRITICAL":
            my_logger.critical(msg)
        #移除输出渠道 removeHandler()
        my_logger.removeHandler(fh)#一定要移除输出渠道
        my_logger.removeHandler(sh)
        fh.close()
    def debug(self,msg):
        '''具体到debug等级，取相对应日志的提示'''
        self.log_info("debug",msg)
    def info(self,msg):
        '''具体到info等级，取相对应日志的提示'''
        self.log_info("info",msg)
    def warning(self,msg):
        '''具体到warning等级，取相对应日志的提示'''
        self.log_info("warning",msg)
    def error(self,msg):
        '''具体到error等级，取相对应日志的提示'''
        self.log_info("error",msg)
    def critical(self,msg):
        '''具体到debug等级，取相对应日志的提示'''
        self.log_info("critical",msg)
if __name__ == '__main__':
    myLog().debug("日志的等级是debug")

